// pages/member/pick_up_card_order/pick_up_card_order.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    scrollTop: 0,
    //当前的订单状态
    selected: 0,
    isLoadMore: true,
    //shop 商城，cashier 收银台 store 门店
    orderType: 'pickup_card',
    detailUrl: 'orderdetail',
    total_page: 0,
    page: 1,
    action: '',
    //全部订单
    orderList: [],
    //待付款
    waitPayList: [],
    //待发货
    waitSendList: [],
    //全待收货
    waitReceiveList: [],
    //已完成
    waitCompleteList: [],
    stopRefresh: false,
    //待付款-多订单合并付款的订单id数组
    order_ids: [],
    //是否开启合并支付
    isMultiplePay: false,

    menu_group:{},

    plugin_name: "旅游激活卡",
    apiArr: [
      "plugin.picking-card.frontend.PickingOrder.index",
      "plugin.picking-card.frontend.PickingOrder.waitSend",
      "plugin.picking-card.frontend.PickingOrder.waitReceive",
      "plugin.picking-card.frontend.PickingOrder.completed",
      "plugin.picking-card.frontend.PickingOrder.refund"
    ]
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.status) {
      this.setData({
        selected: options.status
      });
    }
    if (options.plugin && options.plugin == "one") {
      //#78882，提货卡一期独立的新插件picking-card-one
      wx.setNavigationBarTitle({
        title: '提货卡'
      });
      this.setData({
        orderType: "picking-card-one"
      });
      this.data.apiArr = [
        "plugin.picking-card-one.frontend.PickingOrder.index",
        "plugin.picking-card-one.frontend.PickingOrder.waitSend",
        "plugin.picking-card-one.frontend.PickingOrder.waitReceive",
        "plugin.picking-card-one.frontend.PickingOrder.completed",
        "plugin.picking-card-one.frontend.PickingOrder.refund"
      ];
    } else {
      wx.setNavigationBarTitle({
        title: '旅游激活卡订单'
      });
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {


  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this._getOrderList(this.data.selected);
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {
    if (this.data.stopRefresh) {
      wx.stopPullDownRefresh();
    } else {
      this._initData();
      this._getOrderList(this.data.selected);
    }

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData(this.data.page, this.data.selected);
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _initData() {
    this.setData({
      isLoadMore: true
    });
    this.data.page = 1;
    this.data.total_page = 0;
  },
  _getOrderList(index) {
    this.data.action = this.data.apiArr[index];
    this._getNetData(this.data.action, index);
  },
  //获取订单数据
  _getNetData(api, index) {
    let that = this;
    let urlStr = app.getNetAddresss(api);
    urlStr += "&page=" + 1;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        console.log(index, '测试');

        if (res.result == 1) {
          let myData = res.data;
          that.setData({
            menu_group:res.data.menu_group
          });
          that.data.total_page = myData.total;
          if (index == 0) {
            that.setData({
              orderList: myData.data
            });
          } else if (index == 1) {
            that.setData({
              waitPayList: myData.data
            });
          } else if (index == 2) {
            that.setData({
              waitSendList: myData.data
            });
          } else if (index == 3) {
            that.setData({
              waitReceiveList: myData.data
            });
          } else if (index == 4) {
            that.setData({
              waitCompleteList: that.data.waitCompleteList.concat(myData.data)
            });
          } else {
            that.setData({
              waitRefund: myData.data
            });
          }
        }
        wx.stopPullDownRefresh();
      },
      fail: function(res) {
        wx.stopPullDownRefresh();
        console.log(res);
      }
    });
		

  },
  _getMoreData(page, index) {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss(this.data.action);
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data;
            if (index == 0) {
              that.setData({
                orderList: that.data.orderList.concat(myData.data)
              });

            } else if (index == 1) {
              that.setData({
                waitPayList: that.data.waitPayList.concat(myData.data)
              });

            } else if (index == 2) {
              that.setData({
                waitSendList: that.data.waitSendList.concat(myData.data)
              });

            } else if (index == 3) {
              that.setData({
                waitReceiveList: that.data.waitReceiveList.concat(myData.data)
              });
            } else if (index == 4) {
              that.setData({
                waitCompleteList: that.data.waitCompleteList.concat(myData.data)
              });
            } else {
              that.setData({
                waitRefund: that.waitRefund.concat(myData.data)
              });
            }
          }

        },
        fail: function(res) {
          console.log(res);

        }
      });
    }
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index
    });
    this._initData();
    this._getOrderList(this.data.selected);
  },
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    });
  },
  stopRefresh(e) {
    let bol = e.detail;
    this.setData({
      stopRefresh: bol
    });
  },
  gotoPickUpCardMy(){
    wx.navigateTo({
      url: '/packageE/pickUpCard/pickUpCardMy/pickUpCardMy'
    });
  },
  //接收到合并多订单支付的通知方法
  multiplePayAction(e) {
    let order_ids = e.detail;
    this.setData({
      order_ids: order_ids
    });
    if (order_ids.length > 0) {
      this.setData({
        isMultiplePay: true
      });
    } else {
      this.setData({
        isMultiplePay: false
      });
    }
  },
  gotoPickUpCard(){
    if(this.data.orderType == "picking-card-one") {
      wx.navigateTo({
        url: '/packageE/pickUpCard/pickUpCardIndex/pickUpCardIndex?plugin=one'
      });
      return;
    }
    wx.navigateTo({
      url: '/packageE/pickUpCard/pickUpCardIndex/pickUpCardIndex'
    });
  },
  //待付款状态下-多订单合并支付,order_ids-订单id数组
  toMultiplePay() {
    //type-1订单支付，type-2订单合并支付
    wx.navigateTo({
      url: '/packageD/buy/orderPay/orderPay?order_id=' + this.data.order_ids.join(',') + '&status=2'
    });
  },
  //确认收货订单
  cofirmOrderAction(e) {
    let order = e.detail;
    if (this.data.selected == 0) {
      //全部订单列表
      this._getOrderList(this.data.selected);
    } else if (order == 'pay') {
      this._getOrderList(this.data.selected);
    } else {
      let waitReceiveList = this.data.waitReceiveList;
      for (let i = 0; i < waitReceiveList.length; i++) {
        if (order == waitReceiveList[i].id) {
          waitReceiveList.splice(i, 1);
        }
      }
      this.setData({
        waitReceiveList: waitReceiveList
      });

    }
  },
});
